<?php
/**
 * 专营店端项目管理
 * @author qianxun
 *
 */
class StorePro extends MY_Controller{
    function StorePro(){
        parent::MY_Controller();
    }
    /**
     * 专营店端管理列表
     */
    function index(){
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('StorePro', 'index'),'title'=>'专营店反馈列表');
        $status = (int)$this->input->get('status');
        $status || $status = 1;//默认审核状态
        $this->viewData['status'] = $status;
        //查询条件
        $where = array('isDel'=>0,'status'=>1);
        //年份
        $year = (int)$this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        $year && $where['year'] = $year;
        //时段
        $timeUnit = (int)$this->input->get('timeUnit');
        $this->viewData['timeUnit'] = $timeUnit;
        $timeUnit && $where['timeUnit'] = $timeUnit;
        //查询项目数据
        $this->load->model('projectModel');
        $proList = $this->projectModel->getNewList($where, NULL, NULL, '', 'proId desc');
        //查询专营店项目数据
        $this->load->model('storeProModel');
        $storeProList = $this->storeProModel->getProListKeyProId($this->user['userId']);
        //筛选出各审核状态的数据
        $list1 = array();$list2 = array();$list3 = array();
        foreach ($proList as $v){
            $v['proId'] = (int)$v['proId'];
            if (isset($storeProList[$v['proId']])){
                $storeProData = $storeProList[$v['proId']];
                if ($storeProData['auditRs'] == -1){//未提交
                    $list1[] = $v;
                }else if ($storeProData['auditRs'] == 0){//待审核
                    $v['storeData'] = $storeProData;
                    $list2[] = $v;
                }else{//已审核
                    $v['storeData'] = $storeProData;
                    $list3[] = $v;
                }
            }else{
                $list1[] = $v;
            }
        }
        $this->viewData['list1'] = $list1;
        $this->viewData['list2'] = $list2;
        $this->viewData['list3'] = $list3;
        $this->viewData['storeProStatus'] = $this->config->item('storeProStatus');
        $this->load->view("StorePro/index", $this->viewData);
    }
    /**
     * 立即填写初始化
     */
    function initTo(){
        $proId = (int)$this->input->get('proId');
        $proId || showError('请选择反馈项目');
        $this->load->model('projectModel');
        $proData = $this->projectModel->getNewData(array('proId'=>$proId,'isDel'=>0,'status'=>1));
        $proData['endTime'] > time() || showError('项目已结束,不能提交');
        $this->load->model('storeProModel');
        $findData = $this->storeProModel->getData(array('storeId'=>$this->user['userId'],'proId'=>$proId));
        if (!$findData){
            $data = array(
                'year'=>$proData['year'],
                'timeUnit' => $proData['timeUnit'],
                'proId' => $proId,
                'storeId' => $this->user['userId'],
                'itemValue' => serialize(array()),
                'auditValue' => serialize(array()),
                'auditRs' => -1,
                'createTime' => time(),
            );
            $id = $this->storeProModel->add($data);
            if ($id){
                redirect(printUrl('StorePro', 'apply', array('id'=>$id)));
            }else{
                showError('初始化数据失败');
            }
        }else{
            if ($findData['auditRs'] == -1){//待填写
                redirect(printUrl('StorePro', 'apply', array('id'=>$findData['id'])));
            }else{//已提交或已审核
                redirect(printUrl('StorePro', 'check', array('id'=>$findData['id'])));
            }
        }
    }
    /**
     * 填写
     */
    function apply(){
        if ($_POST){
            $proId = (int)$this->input->post('proId');
            $this->load->model('proActItemModel');
            $actItemList = $this->proActItemModel->getActItem($proId);
            $itemValue = $this->input->post('actItem');
            foreach ($actItemList as $v){
                if ($v['isRequired']){
                    if (in_array($v['type'], array(1,2,5)) && (!isset($itemValue[$v['itemId']]) || empty($itemValue[$v['itemId']]) )){
                        showError('题目【'.$v['itemTitle'].'】还未输入答案');
                    }elseif (in_array($v['type'], array(3,4)) && (!isset($itemValue[$v['itemId']]) || empty($itemValue[$v['itemId']]) )){
                        showError('题目【'.$v['itemTitle'].'】还未选择答案');
                    }elseif (in_array($v['type'], array(6,7)) && (!isset($itemValue[$v['itemId']]) || empty($itemValue[$v['itemId']]['fileId']) )){
                        showError('题目【'.$v['itemTitle'].'】还未上传'.($v['type']==6 ? '图片' : '附件'));
                    }
                }
            }
            $id = (int)$this->input->get('id');
            $id || showError('请选择要提交反馈的数据');
            $data = array(
                'itemValue' => serialize($itemValue),
                'subTime' => time(),
                'auditRs' => 0,
            );
            $this->load->model('storeProModel');
            if ($this->storeProModel->edit(array('id'=>$id,'storeId'=>$this->user['userId']), $data)){
                //更新专营店状态数
                $this->load->model('projectModel');
                $this->projectModel->updateStoreStatusCount($proId);
                
                showSuccess('提交反馈成功', printUrl('StorePro', 'index', array('status'=>2)));
            }else{
                showError('提交反馈失败');
            }
        }else{
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>$_SERVER['HTTP_REFERER'],'title'=>'专营店反馈列表');
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('StorePro', 'apply'),'title'=>'专营店填写');
            
            $id = (int)$this->input->get('id');
            $this->load->model('storeProModel');
            $findData = $this->storeProModel->getData(array('id'=>$id));
            $findData || showError('专营店申报数据不存在');
            
            $this->load->model('projectModel');
            $proData = $this->projectModel->getNewData(array('proId'=>$findData['proId'],'isDel'=>0,'status'=>1));
            $proData || showError('项目数据不存在');
            $proData['endTime'] > time() || showError('项目已结束,不能提交');
            $this->viewData['proData'] = $proData;
            //读取题目数据
            $this->load->model('storeProModel');
            $actList = $this->storeProModel->getActList($id);
            $actList['auditRs'] <= 0 || showError('项目已审核，不能再编辑');
            $this->viewData['actList'] = $actList;
            $this->load->view('StorePro/apply', $this->viewData);
        }
    }
    /**
     * 查看
     */
    function check(){
        $id = (int)$this->input->get('id');
        $id || showError('专营店申报数据不存在');
        $this->load->model('storeProModel');
        $findData = $this->storeProModel->getData(array('id'=>$id));
        
        $this->load->model('projectModel');
        $proData = $this->projectModel->getNewData(array('proId'=>$findData['proId'],'isDel'=>0,'status'=>1));
        $proData || showError('项目数据不存在');
        $this->viewData['proData'] = $proData;
        //读取题目数据
        $this->load->model('storeProModel');
        $this->viewData['actList'] = $this->storeProModel->getActList($id);
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('StorePro', 'index', array('status'=>($findData['status'] == 0?2:3))),'title'=>'专营店反馈列表');
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('StorePro', 'check'),'title'=>'专营店填写');
        $this->load->view('StorePro/check', $this->viewData);
    }
}